﻿/// <reference path="_namespace.js" />
/// <reference path="../components/Component.js" />
/// <reference path="../DomElements.js" />

/*****************************************************************************
Ajax library class definition. Kevin Robinson 2008.
Class: ajax.effects.SoundPlayer
This class attempts to load and play sound across 3 scenarios with this bias:
1) html5 audio tag 2) BGSOUND tag 3) Plug in.
/*****************************************************************************/
ajax.lang.imports("ajax.components.Component");

ajax.lang.addNamespace("ajax.effects");


ajax.effects.SoundPlayer = function(doc, url)
{///<param name="doc" type="domDocument"/>
 ///<param name="url" type="String">The URL of the sound file.</param>
 ///<field name="volume" type="number">A number between 0 and 100. Higher is louder. Default is 100.</field>
    ajax.effects.SoundPlayer.superConstructor.call(this, doc);
    this.url = url;
    // if NO audio tag and IE or Opera, use BGSOUND.
    this.bgSoundMode = !ajax.effects.SoundPlayer.hasAudio && (ajax.Browser.isInternetExplorer || ajax.Browser.isOpera);
}
//-----------------------------------------------------------------------------
ajax.lang.extend("ajax.effects.SoundPlayer", "ajax.components.Component");
if ($getDesignMode())
    ajax.effects.SoundPlayer.prototype = ajax.components.Component.prototype;

//-----------------------------------------------------------------------------
// Public + private properties
ajax.effects.SoundPlayer.prototype._element = null;
ajax.effects.SoundPlayer.prototype.getElement = function()
{///<returns type="domElement"/>
    return this._element;
}
ajax.effects.SoundPlayer.prototype.url = "";
ajax.effects.SoundPlayer.prototype._playCount = 1;
ajax.effects.SoundPlayer.prototype._loop = 0;
ajax.effects.SoundPlayer.prototype.volume = 100; // 0 to 100
ajax.effects.SoundPlayer.prototype.bgSoundMode = false;

// Statics...
ajax.effects.SoundPlayer.hasAudio = !!document.createElement('audio').canPlayType;

//-----------------------------------------------------------------------------
// Private methods
ajax.effects.SoundPlayer.prototype._createElement = function()
{
    if (this._element)
        this.removeNode(this._element);

    if (this.bgSoundMode) //IE or Opera
    {
        this._element = this.getDocument().createElement("bgsound");
    }
    else
    {
        if (ajax.effects.SoundPlayer.hasAudio)
        {
            this._element = this.getDocument().createElement("audio");
            // makes sense to load the audio in ready to use.
            this._element.setAttribute("preload", "auto");
        }
        else
        {
            // No audio tag, not ie or opera...embed is only option left...
            var width = 170; var height = 45;
            this._element = this.getDocument().createElement("embed");
            this._element.style.position = "absolute";
            this._element.style.left = parseInt(-width) + "px";
            this._element.style.top = parseInt(-height) + "px";
            this._element.setAttribute("width", width);
            this._element.setAttribute("height", height);
            this._element.setAttribute("hidden", false);
        }
    }
    
    //((100-x)*100)
    this._element.setAttribute("src", this.url);
    
    // play() now takes care of this.
    //this._element.setAttribute("autoplay", true); // Audio
    //this._element.setAttribute("autostart", true); // FF media
    
    this._element.setAttribute("type", this.getMimeType());
    this._element.setAttribute("enablejavascript", true);

    // Set initial volume
    this.setLoop(this._loop);
    this.setVolume(this.volume);
}

ajax.effects.SoundPlayer.prototype.dispose = function()
{
    this.stop();
    // Remove the DOM element (stopping any sounds)
    this.removeNode(this._element);
    ajax.effects.SoundPlayer.superClass.dispose.call(this);
}

// Public methods
ajax.effects.SoundPlayer.prototype.getMimeType = function()
{    
    var mimeType = "application/x-mplayer2"; //default
    var agt= ajax.Browser.userAgent.toLowerCase();    
    if (ajax.Browser.navigator.mimeTypes && agt.indexOf("windows")==-1) 
    {
        //non-IE, no-Windows
        var plugin=navigator.mimeTypes["audio/mpeg"].enabledPlugin;
        if (plugin) mimeType="audio/mpeg" //Mac/Safari & Linux/FFox
    }//end no-Windows
    return mimeType;
}

ajax.effects.SoundPlayer.prototype.stop = function()
{    
    try
    {
        // No audio but IE
        if (!ajax.effects.SoundPlayer.hasAudio)
        {
            this._element.stop(); 
            this._element.rewind();
        }
        else // Has audio
        {
            this._element.pause();
            this._element.currentTime = 0;
        }
    }
    catch(ex)
    {            
        // FF
        this.removeNode(this._element);
        this._element = null;
    }    
}

ajax.effects.SoundPlayer.prototype.pause = function()
{///<summary>Pauses the current sound.</summary>
    try
    {
        this._element.pause();
    }
    catch(ex){}
}

ajax.effects.SoundPlayer.prototype.play = function()
{///<summary>Plays the sound with any looping values set.</summary>
    if (this._element == null)
    {
        this._createElement();
    }

    try
    {
        // ie        
        this._element.play();
    }
    catch (ex)
    {
        // play() method not supported. Try adding to the dom document instead.
        if (!ajax.effects.SoundPlayer.hasAudio)
        {
            this._element.setAttribute("autoplay", true);
            this.getDocument().body.appendChild(this._element);
        }
    }
}

ajax.effects.SoundPlayer.prototype.setVolume = function(val)
{///<summary>This only seems to work on IE</summary>
    
    val = Math.min(Math.max(val, 0), 100);
    this.volume = val;   
    
    if (!this._element)
        return;
        
    // Apply to element (if exists)
    if (this._element.tagName == "BGSOUND")
    {  // ie
        var vol =-((100-this.volume)*100);
        this._element.setAttribute("volume", vol);
        return;
    }
    // everyone else
    this._element.setAttribute("volume", this.volume);               
}

ajax.effects.SoundPlayer.prototype.setLoop = function(numLoops)
{///<summary>-1 = infinite, 0 = none, n = number of times to loop (not usually supported).</summary>
    this._loop = numLoops;
    this._playCount = numLoops;

    // Wipe out the playcount value if set to infinite loops and NOT ie.
    if (!ajax.Browser.isInternetExplorer)
    {
        if (numLoops == -1)
        {
            this._playCount = "";
        }

        if (numLoops == 0)
            this._playCount = 1;
    }

    if (!this._element)
        return;

    // Non audio...
    if (!ajax.effects.SoundPlayer.hasAudio)
    {
        this._element.setAttribute("loop", this._loop);
        this._element.setAttribute("playcount", this._playCount);
    }
    else // Audio accepts only true or false
    {
        this._element.setAttribute("loop", this._loop == 0 ? false : true);
    }
}


//-----------------------------------------------------------------------------
ajax.effects.SoundPlayer.createTiefighter = function(doc)
{///<returns type="ajax.effects.SoundPlayer">Returns a tie fighter sound.</returns>
    var sp = new ajax.effects.SoundPlayer(doc, ajax.getDomain("/media/audio/Tiefighter.wav"));
    return sp;
}

// Statics
//-----------------------------------------------------------------------------
ajax.effects.SoundPlayer.cast = function(obj)
{///<returns type="ajax.effects.SoundPlayer"/>
    return obj;
}
